﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using TransactionServer.ActivityDesign;
using System.ComponentModel;
using TransactionServer.General_Classes;
using Microsoft.VisualBasic.Activities;

namespace TransactionServer.CRM4Activities
{
    [Designer(typeof(TransactionServer.ActivityDesign.GetEntities))]
    public sealed class CRM4GetEntities : CodeActivity
    {
        // Define an activity input argument of type string
        public InArgument<Context> Context { get; set; }
        public InArgument<String> Connection { get; set; }
        public InArgument<String> Entity { get; set; }
        public InArgument<Filter> Filter { get; set; }

        public OutArgument<List<Entity>> Entities { get; set; }

        public CRM4GetEntities()
        {
            VisualBasicValue<Context> vbArgument = new VisualBasicValue<Context>(Constants.CONTEXT_NAME);
            this.Context = InArgument<Context>.FromExpression(vbArgument);
        }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            Context con = context.GetValue(this.Context);
            Filter filter = context.GetValue(this.Filter);
            string entity = context.GetValue(this.Entity);
            string connection = context.GetValue(this.Connection);

            filter.connection = connection;
            filter.entity = entity;

            CRM4Connector conn = (CRM4Connector)con.Connector[connection];

            context.SetValue(this.Entities, conn.getObjects(filter));
        }
    }
}
